﻿using TrueSync;
[System.Serializable]
public class JumpState : StateBase
{
    public FP JumpSpeed;
    public FP JumpAngle;

    LogicMoveComponent mMoveComponent;
    int mFrame;

    public override StateEnum StateName { get { return StateEnum.Jump; } }
    public override void Enter(LogicEntity lEntity, StateBase lastState, object[] stateParams)
    {
        base.Enter(lEntity, lastState, stateParams);
        mMoveComponent = mLEntity.GetLogicComponent<LogicMoveComponent>();
        mMoveComponent.MoveSpeed = new TSVector2(
            JumpSpeed * FP.FastSin(JumpAngle * FP.Deg2Rad) * mMoveComponent.FaceDirection,
            JumpSpeed * FP.FastCos(JumpAngle * FP.Deg2Rad));
        mFrame = 0;
    }

    public override bool IsFinished()
    {
        return mMoveComponent.Position.y <= 0 && mFrame > 0;
    }

    public override void Excute(FP deltaTime)
    {
        if (statusComponent.HP <= 0)
        {
            mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Dead);
            return;
        }

        if (statusComponent.Dizzy <= 0)
        {
            mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Dizzy);
            return;
        }

        if (IsFinished())
        {
            mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Stand);
        }

        mFrame++;
    }

    public override void Exit()
    {
        TSVector2 position = mMoveComponent.Position;
        mMoveComponent.Position = new TSVector2(position.x, 0);
        mMoveComponent = null;
        base.Exit();
    }

    public override void OnStimulate(Stimulate stimulate)
    {
        LogicStateMachineComponent stateMachineComponent = mLEntity.GetLogicComponent<LogicStateMachineComponent>();
        switch (stimulate.StimulateType)
        {
            case StimulateTypeEnum.HitFloatStimulate:
                HitFloatStimulate hitfloatStimulate = (HitFloatStimulate)stimulate;
                stateMachineComponent.ChangeState(StateEnum.FloatHit, hitfloatStimulate.XMoveLength, hitfloatStimulate.XMoveSpeed, hitfloatStimulate.YHeight);
                break;
            case StimulateTypeEnum.FalldownStimulate:
            case StimulateTypeEnum.HitFlyStimulate:
            case StimulateTypeEnum.HitBackStimulate:
                HitBackStimulate hitbackStimulate = (HitBackStimulate)stimulate;
                stateMachineComponent.ChangeState(StateEnum.JumpHit);
                break;
            case StimulateTypeEnum.ThrowStimulate:
                stateMachineComponent.ChangeState(StateEnum.ThrowHit);
                break;
        }
    }

    public override void Serialize(EByte ms)
    {
        base.Serialize(ms);
        ms.WriteLong(JumpSpeed.RawValue);
        ms.WriteLong(JumpAngle.RawValue);
    }

    public override void Deserialize(EByte ms)
    {
        base.Deserialize(ms);
        JumpSpeed = FP.FromRaw(ms.ReadLong());
        JumpAngle = FP.FromRaw(ms.ReadLong());
    }
}
